<template>
  <div>
    <!-- 门店筛选 + 新增按钮 -->
    <el-form :inline="true" style="margin-bottom:12px">
      <el-form-item label="门店">
        <el-select v-model="storeId" clearable placeholder="全部" style="width:160px">
          <el-option
            v-for="s in storeList"
            :key="s.id"
            :label="s.storeName"
            :value="s.id"
          />
        </el-select>
      </el-form-item>
      <el-button type="primary" @click="openAdd">新增员工</el-button>
    </el-form>

    <!-- 员工列表 -->
    <el-table :data="waiters" stripe>
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="phone" label="电话" />
      <el-table-column label="状态">
        <template #default="scope">
          <el-switch
            v-model="scope.row.isActive"
            @change="toggle(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增弹窗 -->
    <el-dialog v-model="dialog" title="新增员工" width="400px">
      <el-form :model="form" label-width="80">
        <el-form-item label="门店">
          <el-select v-model="form.storeId">
            <el-option
              v-for="s in storeList"
              :key="s.id"
              :label="s.storeName"
              :value="s.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialog=false">取消</el-button>
        <el-button type="primary" @click="submitAdd">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import request from '@/api/request'
import { ElMessage } from 'element-plus'

interface Waiter {
  id: number
  username: string
  phone: string
  isActive: boolean
  storeId: number | null
}
interface Store {
  id: number
  storeName: string
}

const storeList = ref<Store[]>([])
const waiters   = ref<Waiter[]>([])
const storeId   = ref<number | null>(null)
const dialog    = ref(false)
const form      = ref({ storeId: null as any, name: '', phone: '' })

/* 获取门店列表 */
const loadStores = () =>
  request.get('/admin/stores/page', { params: { size: 999 } })
    .then(res => (storeList.value = res.data.records))

/* 获取员工列表（前端过滤） */
const loadWaiters = () =>
  request.get('/admin/waiters').then(res => {
    waiters.value = res.data.filter((w: Waiter) =>
      storeId.value == null || w.storeId === Number(storeId.value)
    )
  })

/* 新增员工 */
const openAdd = () => {
  form.value = { storeId: storeList.value[0]?.id || null, name: '', phone: '' }
  dialog.value = true
}
const submitAdd = () => {
  request.post(
    `/auth/waiter?storeId=${form.value.storeId}&name=${encodeURIComponent(
      form.value.name
    )}&phone=${form.value.phone}&password=123456`
  ).then(() => {
    ElMessage.success('已添加')
    dialog.value = false
    loadWaiters()
  })
}

/* 启用/禁用 */
const toggle = (row: Waiter) =>
  request.patch(`auth/waiter/${row.id}?active=${row.isActive}`)
    .catch(() => (row.isActive = !row.isActive))

onMounted(() => { loadStores(); loadWaiters() })
watch(storeId, loadWaiters)
</script>